package com.tencreat.account.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.annotation.RepeatSubmit;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelNewUtil;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.account.MallSupplier;
import com.tencreat.domain.account.dto.MallSupplierRequDTO;
import com.tencreat.domain.account.vo.MallSupplierRespVo;
import com.tencreat.domain.account.dto.MallSupplierDTO;
import com.tencreat.domain.account.vo.MallSupplierStaticsVo;
import com.tencreat.domain.mall.dto.MallSupplierAuditDTO;
import com.tencreat.domain.mall.vo.MallUserVO2;
import com.tencreat.service.account.IMallRegionService;
import com.tencreat.service.account.IMallSupplierService;
import com.tencreat.service.account.IMallUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags = "供应商api")
@RestController
@RequestMapping("/mall/supplier")
public class AdminMallSupplierController extends BaseController {
    @Autowired
    private IMallSupplierService supplierService;
    @Autowired
    private IMallUserService userService;
    @Autowired
    private IMallRegionService mallRegionService;

    /**
     * 查询供应商列表
     */
    @ApiOperation(value = "查询供应商列表")
    @PreAuthorize("@ss.hasPermi('mall:supplier:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallSupplierRespVo>> list(@RequestBody MallSupplier mallSupplier) {
        Page<MallSupplier> page = new Page<>(mallSupplier.getPageNum(), mallSupplier.getPageSize());
        return HttpResult.ok(supplierService.queryAll(page,mallSupplier));
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增供应商")
    @PreAuthorize("@ss.hasPermi('mall:supplier:add')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public HttpResult<String> add(@RequestBody MallSupplierDTO mallSupplierVO) {
        return supplierService.insertMallSupplier(mallSupplierVO);
    }



    /**
     * 导出供应商列表
     */
    @ApiOperation(value = "导出供应商列表")
    @PreAuthorize("@ss.hasPermi('mall:supplier:export')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody MallSupplier mallSupplier) {
        Page<MallSupplier> page = new Page<>(1, Integer.MAX_VALUE);
        IPage<MallSupplierRespVo> supplierPage = supplierService.queryAll(page, mallSupplier);


//        List<MallSupplierRespVo> list = supplierPage.getRecords();
        supplierPage.convert(item->{
            MallSupplier mallSupplier1 = new MallSupplier();
            BeanUtils.copyProperties(item, mallSupplier1);
            mallSupplier1.setAreasName(item.getProvinceName()+item.getCityName()+item.getCountyName());
            mallSupplier1.setApplyTime(item.getCreateTime());

            return mallSupplier1;
        });
        List<MallSupplier> list = JSONObject.parseArray(JSONObject.toJSONString(supplierPage.getRecords()), MallSupplier.class);
        ExcelUtil<MallSupplier> util = new ExcelUtil<MallSupplier>(MallSupplier.class);
        return util.exportExcel(list, "partner");
    }

    /**
     * 获取供应商详细信息
     */
    @ApiOperation(value = "获取供应商详细信息")
    @PreAuthorize("@ss.hasPermi('mall:supplier:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallSupplier> getInfo(@RequestBody ReqIdParam id) {
        return HttpResult.ok(supplierService.selectMallSupplierById(id));
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/forget_password")
    public HttpResult<Boolean> forgetPassword( @RequestBody MallSupplierRequDTO mallSupplier) {
        return supplierService.password(mallSupplier);
    }

    /**
     * 修改供应商
     */
    @ApiOperation(value = "修改供应商")
    @PreAuthorize("@ss.hasPermi('mall:supplier:edit')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<String> edit(@RequestBody MallSupplier mallSupplier) {
        return supplierService.updateMallSupplier(mallSupplier);
    }

    @ApiOperation(value = "供应商审核")
    @PreAuthorize("@ss.hasPermi('mall:supplier:audit')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.AUDIT)
    @PostMapping(value = "/audit")
    @RepeatSubmit
    public HttpResult<String> audit(@RequestBody @Validated MallSupplierAuditDTO dto) {
        supplierService.audit(dto);
        return HttpResult.ok();
    }

    /**
     * 删除供应商
     */
    @ApiOperation(value = "删除供应商")
    @PreAuthorize("@ss.hasPermi('mall:supplier:remove')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(supplierService.deleteMallSupplierByIds(ids));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:supplier:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("users", userService.selectpartner());
        if (StringUtils.isNotNull(userId))
        {
            ajax.put(AjaxResult.DATA_TAG, supplierService.selectMallSupplierById(userId));
        }
        return ajax;
    }

    /**
     * 供应商统计
     */
    @ApiOperation(value = "供应商统计")
    @PreAuthorize("@ss.hasPermi('mall:supplier:list')")
    @GetMapping("/supplierStatics")
    public HttpResult<MallSupplierStaticsVo> supplierStatics() {
        return HttpResult.ok(supplierService.supplierStatics());
    }
}
